<template>
  <el-menu
    :default-active="$route.path"
    class="el-menu-vertical-demo"
    :collapse="isCollapse"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <h4>{{ isCollapse ? "后台" : "通用后台管理系统" }}</h4>
    <el-menu-item
      v-for="item in nochildren"
      :index="item.path + ''"
      :key="item.path"
      @click="clickMenu(item)"
    >
      <i :class="'el-icon-' + item.icon"></i>
      <span slot="title"> {{ item.lable }}</span>
    </el-menu-item>
    <el-submenu
      v-for="item in haschildren"
      :index="item.path + ''"
      :key="item.path"
    >
      <template slot="title">
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.lable }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.path">
        <el-menu-item :index="subItem.path + ''" @click="clickMenu(subItem)">{{
          subItem.lable
        }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  name: "CommonAside",
  data() {
    return {
      menu: [
        {
          path: "/home",
          name: "home",
          lable: "首页",
          icon: "s-home",
          url: "Home/Home",
        },
        {
          path: "/mall",
          name: "mall",
          lable: "商品管理",
          icon: "video-play",
          url: "mallManage/mallManage",
        },
        {
          path: "/user",
          name: "user",
          lable: "用户管理",
          icon: "user",
          url: "userManage/userManage",
        },
        {
          lable: "其他",
          path: "",
          icon: "location",
          children: [
            {
              path: "/page1",
              name: "page1",
              lable: "页面一",
              icon: "setting",
              url: "Other/PageOne",
            },
            {
              path: "/page2",
              name: "page2",
              lable: "页面二",
              icon: "setting",
              url: "Other/PageTwo",
            },
          ],
        },
      ],
    };
  },
  methods: {
    clickMenu(item) {
      if (this.$route.name !== item.name)
        this.$router.push({ name: item.name });
      // console.log(this.$route);
      this.$store.commit("selectMenu", item);
    },
  },
  computed: {
    nochildren() {
      return this.menu.filter((item) => !item.children);
    },
    haschildren() {
      return this.menu.filter((item) => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
  },
};
</script>

<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-menu {
  height: 100%;
  border: none;
}
.el-menu > h4 {
  color: #fff;
  width: auto;
  text-align: center;
  line-height: 48px;
}
</style>
